package com.yiwei.zxp.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.SmsSendBean;
import com.yiwei.zxp.api.bean.ThirdBindBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class WechatBindActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_phone_clear)
    ImageView btnPhoneClear;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_code_clear)
    ImageView btnCodeClear;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.txt_agree)
    TextView txtAgree;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_pass_login)
    TextView btnPassLogin;

    private Unbinder unbiner;
    private String phone;
    private boolean isSendCode = true;
    private static final int TIME = 60 * 1000;  //验证码倒计时时间
    private CountDownTimer timer = new CountDownTimer(TIME, 1000) {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onTick(long millisUntilFinished) {
            btnSendCode.setText("重新获取" + (millisUntilFinished / 1000) + "s");
            if (isSendCode) {
                isSendCode = false;
                btnSendCode.setEnabled(false);
                btnSendCode.setBackgroundResource(R.drawable.btn_send_code_uncheck);
                btnSendCode.setTextColor(0xFFFFFFFF);
            }

        }

        @Override
        public void onFinish() {
            isSendCode = true;
            resetBtn();
        }
    };
    private String tp_id;
    private String plat;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void resetBtn() {
        if (timer != null) timer.cancel();
        btnSendCode.setEnabled(true);
        btnSendCode.setText("发送验证码");
        btnSendCode.setTextColor(0xFFFF0030);
        btnSendCode.setBackgroundResource(R.drawable.btn_send_code_check);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat_bind);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("联合登录绑定");
        txtRight.setText("跳过");
        txtRight.setVisibility(View.VISIBLE);
        UI.setTvHtml(txtAgree, R.string.wechat_agree);
        tp_id = getIntent().getStringExtra("tp_id");
        plat = getIntent().getStringExtra("plat");
    }

    public void getSmsSend(String phone) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        SmsSendBean bean = new SmsSendBean();
        bean.phone = phone;
        Api.api_service.getForgetSend(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "验证码已发送");
                    timer.start();
                });
    }

    public void getBind(String tp_type, String tp_id, String phone, String code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ThirdBindBean bean = new ThirdBindBean();
        bean.phone = phone;
        bean.code = code;
        bean.tp_id = tp_id;
        bean.tp_type = tp_type;
        Api.api_service.getThirdBind(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "绑定成功");
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_phone_clear, R.id.btn_code_clear, R.id.btn_send_code, R.id.btn_submit, R.id.btn_pass_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_phone_clear:
                editPhone.setText("");
                break;
            case R.id.btn_code_clear:
                editCode.setText("");
                break;
            case R.id.btn_send_code:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空");
                    return;
                }
                getSmsSend(UI.toString(editPhone));
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(this, "验证码不能为空");
                    return;
                }
                getBind(plat, tp_id, UI.toString(editPhone), UI.toString(editCode));
                break;
            case R.id.btn_pass_login:
                finish();
                break;
        }
    }
}
